from rest_framework.decorators import action

from dvadmin.system.models import Complaints
from dvadmin.utils.json_response import DetailResponse
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.viewset import CustomModelViewSet


class ComplaintsSerializer(CustomModelSerializer):
    """
    用户投诉接口-序列化器
    """

    class Meta:
        model = Complaints
        fields = "__all__"
        read_only_fields = ["id"]


class ComplaintsCreateUpdateSerializer(CustomModelSerializer):
    """
    用户投诉接口 创建/更新时的列化器
    """

    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = Complaints
        fields = '__all__'


class ComplaintsViewSet(CustomModelViewSet):
    """
    用户投诉接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = Complaints.objects.all()
    serializer_class = ComplaintsSerializer

    @action(methods=['GET'], detail=True)
    def role_get_menu(self, request):
        app_user_queryset = Complaints.objects.filter(users__id=request.user.id).values_list('data_range', flat=True)
        return DetailResponse(data=app_user_queryset)
